import subprocess
import os

def open_ppt(ppt_path, wps_path=None):
    if not os.path.exists(ppt_path):
        raise FileNotFoundError(f"文件不存在: {ppt_path}")

    if not wps_path:
        possible_paths = [
            r"C:\Program Files\Kingsoft\WPS Office\ksolaunch.exe",
            r"C:\Program Files (x86)\Kingsoft\WPS Office\ksolaunch.exe",
            r"C:\Program Files\WPS Office\ksolaunch.exe",
            r"C:\Program Files (x86)\WPS Office\ksolaunch.exe",
            os.path.expandvars(r"%USERPROFILE%\AppData\Local\Kingsoft\WPS Office\wps.exe")
        ]
        for path in possible_paths:
            if os.path.exists(path):
                wps_path = path
                break

    if not wps_path or not os.path.exists(wps_path):
        ppt_path = os.path.abspath(os.path.join('running', 'running.pptx'))
        if os.path.exists(ppt_path):
            try:
                os.remove(ppt_path)
                print(f"已删除临时文件：{ppt_path}")
            except Exception as e:
                print(f"删除临时文件失败：{e}")
        raise FileNotFoundError("未找到WPS Office，安装WPS Office或指定wps_path")


    try:
        subprocess.Popen([wps_path, ppt_path], shell=True)
        print(f"成功打开文件: {ppt_path}")
    except Exception as e:
        print(f"打开文件失败: {e}")

def open_ppt_with_default_app(ppt_path):
    if not os.path.exists(ppt_path):
        raise FileNotFoundError(f"文件不存在: {ppt_path}")
    try:
        os.startfile(ppt_path)
    except Exception as e:
        print(f"打开文件失败: {e}")